home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / ABox 1.9.5 / CPlus Files / ABUText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-26  |  13.6 KB  |  576 lines  |  [TEXT/MMCC]

  1. /*    
  2.     Copyright © 1991-1995 by TopSoft Inc.  All rights reserved.
  3.  
  4.     You may distribute this file under the terms of the TopSoft
  5.     Artistic License, accompanying this package.
  6.     
  7.     This file was developed by George (ty) Tempel in connection with TopSoft, Inc..
  8.     See the Modification History for more details.
  9.  
  10.     This file is based upon work produced by Greg Anderson (Apple DTS).
  11.  
  12. Product
  13.     About Box
  14.  
  15. FILE
  16.     ABUText.c
  17.  
  18. NAME
  19.     ABUText.c, part of the ABox project source code,
  20.     responsible for mix-in handling the AboutBox text
  21.     utilities stuff, such as scroll bar synchronization, line
  22.     heights, etc.
  23.  
  24. DESCRIPTION
  25.     This file contains defines for the about box modules.
  26.     
  27. DEVELOPED BY
  28.     George (ty) Tempel                ttempel@monmouth.com
  29.     All code in this file, and its associated header file was
  30.     Created by George (ty) Tempel in connection with the TopSoft, Inc.
  31.     "FilterTop" application development, except where noted.
  32.  
  33. CARETAKER - George (ty) Tempel <ttempel@monmouth.com>
  34.      Please consult this person for any changes or suggestions to this file.
  35.  
  36. MODIFICATION HISTORY
  37.  
  38.     dd mmm yy    -    xxx    -    patchxx: description of patch
  39.     10 June 94    -    ty    -    Initial Version Created
  40.     20-july-94    -    ty    -    initial version released
  41.     23-may-95    -    ty    -    changes for compatibility with the CodeWarrior CW6
  42.                             release and the associated Universal Headers from Apple:
  43.                             most methods that returned references now have "Ref" at
  44.                             the end of their methods names to prevent possible collisions
  45.                             with datatypes and classes of the same name (older versions
  46.                             of the compiler didn't have a problem with this).
  47.     25-oct-95    -    ty    -    changes for "const" usage under CW7; simplification of Boolean
  48.                             query methods
  49.  
  50. */
  51.  
  52. /*===========================================================================*/
  53.  
  54. /*======= Segmentation directives ========*/
  55.  
  56. #ifdef USE_MANUAL_SEGMENTATION
  57. #pragma segment ty
  58. #endif
  59.  
  60. /*============ Header files ==============*/
  61.     
  62. #include     "ABUText.h"
  63.  
  64. /*=============== Globals ================*/
  65.  
  66.  
  67. /*================ CODE ==================*/
  68.  
  69.  
  70. /*=============================== ABUText::ABUText ================================*/
  71. ABUText::ABUText(void)
  72. {
  73. }    // end ABUText
  74.  
  75.  
  76. /*=============================== ABUText::~ABUText ================================*/
  77. ABUText::~ABUText(void)
  78. {
  79. }    // end ~ABUText
  80.  
  81.  
  82.  
  83.  
  84. /*=============================== ABUText::TEGetSelect ==================================*/
  85. //
  86. // Get the current selection
  87.  
  88. //    is called by:
  89. //        external code; this is a public method
  90. //
  91. void ABUText::TEGetSelect(short* selStart, short* selEnd, TEHandle te)
  92. {
  93.     //    metro: ty...addition to trap bad parameters
  94.     if(!(selStart && selEnd && te))
  95.         return;
  96.  
  97.     *selStart = (*te)->selStart;
  98.     *selEnd    = (*te)->selEnd;
  99. }
  100.  
  101.  
  102. /*=============================== ABUText::TEGetChars ==================================*/
  103. //
  104. // Get some characters from the TextEdit record
  105.  
  106. //    is called by:
  107. //        external code; this is a public method
  108. //
  109. void ABUText::TEGetChars(char *buf, short from, short to, TEHandle te)
  110. {
  111.     CharsHandle        theText = NULL;
  112.     short            theState;
  113.     unsigned char    *ptr;
  114.     
  115.     //    metro: ty...addition to trap bad parameters
  116.     if (!te  || !buf)
  117.         return;
  118.         
  119.     theText = ::TEGetText(te);
  120.  
  121.     if (!theText)
  122.         return;
  123.         
  124.     theState = ::HGetState((Handle) theText);
  125.     ::HLock((Handle) theText);
  126.     ptr = (unsigned char*) (*theText);
  127.     ptr += from; 
  128.     ::BlockMove(ptr, buf, to - from);
  129.     buf[to - from] = 0;
  130.     ::HSetState((Handle) theText, theState);
  131. }
  132.  
  133.  
  134. /*=============================== ABUText::GetTEView ==================================*/
  135. //
  136. // Return the current view rectangle of the given textedit record
  137.  
  138. //    is called by:
  139. //        ViewHeight
  140. //
  141. void ABUText::GetTEView(TEHandle te, Rect *viewRect)
  142. {
  143.     //    metro: ty...addition to trap bad parameters
  144.     if(!(te && viewRect))
  145.         return;
  146.     *viewRect = (*te)->viewRect;
  147. }
  148.  
  149. /*=============================== ABUText::TELines ==================================*/
  150. //
  151. // Return the number of lines in the current textedit buffer
  152. //
  153. //    returns 0 if there are bad parameters.
  154. //
  155. //    is called by:
  156. //        TEHeight
  157. //
  158. short ABUText::TELines(TEHandle te)
  159. {
  160.     //    metro: ty...addition to trap bad parameters
  161.     if (!te)
  162.         return 0;                // 1.0a2 SJ added the 0
  163.     else
  164.         return((*te)->nLines);
  165. }
  166.  
  167. /*=============================== ABUText::TEHeight ==================================*/
  168. //
  169. // Return the pixel height of the current textedit buffer
  170.  
  171. //    is called by:
  172. //        VScrollLocation
  173. //        PinnedTEVScroll
  174. //        TEVThumbScroll
  175. //
  176. short ABUText::TEHeight(TEHandle te)
  177. {
  178.     //    metro: ty...addition to trap bad parameters
  179.     if(!te)        //    metro: ty...addition to trap bad parameters
  180.         return 0;
  181.     else
  182.         return ::TEGetHeight(ABUText::TELines(te), 0, te);
  183. }
  184.  
  185. /*=============================== ABUText::TELineHeight ==================================*/
  186. //
  187. // Return the number of pixels in one line
  188.  
  189. //    is called by:
  190. //        PinnedTEVScroll
  191. //        TEVThumbScroll
  192. //        ScrollTELine
  193. //        ScrollTEPage
  194. //
  195. short ABUText::TELineHeight(TEHandle te)
  196. {
  197.     //    metro: ty...addition to trap bad parameters
  198.     if(!te)                
  199.         return 0;
  200.  
  201.     if((*te)->lineHeight == -1)
  202.         return kTEUdefaultLineHeight;
  203.     else
  204.         return (*te)->lineHeight;
  205. }
  206.  
  207. /*=============================== ABUText::GetTEVScroll ==================================*/
  208. //
  209. //    Return the number of pixels to the current scroll location
  210.  
  211. //    is called by:
  212. //        PinnedTEVScroll
  213. //        TEVThumbScroll
  214. //        VScrollLocation
  215. //
  216. short ABUText::GetTEVScroll(TEHandle te)
  217. {
  218.     //    metro: ty...addition to trap bad parameters
  219.     if(!te)
  220.         return 0;                                        // 1.0a2 SJ added the 0
  221.     else
  222.         return ((*te)->viewRect.top - (*te)->destRect.top);
  223. }
  224.  
  225. /*=============================== ABUText::ViewHeight ==================================*/
  226. //
  227. // Return the pixel height of the view rectangle
  228.  
  229. //    is called by:
  230. //        PinnedTEVScroll
  231. //        VScrollLocation
  232. //        TEVThumbScroll
  233. //        ScrollTEPage
  234. //
  235. short ABUText::ViewHeight(TEHandle te)
  236. {
  237.     Rect    viewRect;
  238.  
  239.     //    metro: ty...addition to trap bad parameters
  240.     if(!te)
  241.         return 0;
  242.         
  243.     ABUText::GetTEView(te, &viewRect);
  244.     
  245.     return (viewRect.bottom - viewRect.top);
  246. }
  247.  
  248. /*=============================== ABUText::VScrollLocation ==================================*/
  249. //
  250. //    Return a number from 1 - 1000 (GetCtlMin() - GetCtlMax()) indicating the 
  251. //    current scroll location of the textedit record
  252.  
  253. //    is called by:
  254. //        FixScrollBar
  255. //
  256. short ABUText::VScrollLocation(ControlHandle scrollbar)
  257. {
  258.     long tmp;
  259.     
  260.     TEHandle    te = (TEHandle) ::GetCRefCon(scrollbar);
  261.     
  262.     //    metro: ty...addition to trap bad parameters
  263.     if (!te)
  264.         return 1;
  265.     tmp = ABUText::GetTEVScroll(te) * (long) ::GetCtlMax(scrollbar);
  266.     tmp /= (ABUText::TEHeight(te) - ABUText::ViewHeight(te));
  267.     
  268.     return tmp;
  269. }
  270.  
  271. /*=============================== ABUText::PinnedTEVScroll ==================================*/
  272. //
  273. //    Call TEScroll, but don't scroll past top or below bottom    
  274. //    This code also adjusts the scrollbar.
  275.  
  276. //    is called by:
  277. //        TEVThumbScroll
  278. //        ScrollTELine
  279. //        ScrollTEPage
  280. //
  281. void ABUText::PinnedTEVScroll(TEHandle te, ControlHandle scrollbar, short dv)
  282. {
  283.     short    max;
  284.     short    lineh = ABUText::TELineHeight(te);
  285.     
  286.     //    metro: ty...addition to trap bad parameters
  287.     if(!(te && scrollbar))
  288.         return;
  289.         
  290.     if(lineh == -1)
  291.         lineh = kTEUdefaultLineHeight;
  292.     
  293.     if(dv > 0) 
  294.     {
  295.         max = ABUText::GetTEVScroll(te);
  296.         if(dv > max)
  297.             dv = max;
  298.     } 
  299.     else 
  300.     {
  301.         max = ABUText::TEHeight(te) - ABUText::ViewHeight(te) - ABUText::GetTEVScroll(te) - 1;
  302.         max = max - (max % lineh) + lineh;
  303.         if(max < 0)
  304.             max = 0;
  305.         if((-dv) > max)
  306.             dv = -max;
  307.     }
  308.     
  309.     ::TEScroll(0,dv,te);
  310.     ABUText::FixScrollBar(scrollbar);
  311. }
  312.  
  313. /*=============================== ABUText::TEVThumbScroll ==================================*/
  314. //
  315. // Scroll directly to a new location.
  316. //
  317. //    is called by:
  318. //        TrackUpDownPage
  319. //        ScrollBarClick
  320. //
  321. void ABUText::TEVThumbScroll(TEHandle te, ControlHandle scrollbar)
  322. {
  323.     long        newScroll;
  324.     short        deltaScroll;
  325.     
  326.     //    metro: ty...addition to trap bad parameters
  327.     if(!(te && scrollbar))
  328.         return;
  329.         
  330.     /*
  331.     // 'GetCtlValue' ranges from 0 to 1000.  Convert this into a textedit range
  332.     // of zero to (texteditHeight - viewHeight)
  333.     */
  334.     
  335.     newScroll  = ::GetCtlValue(scrollbar);
  336.     newScroll *= (ABUText::TEHeight(te) -ABUText:: ViewHeight(te));
  337.     newScroll /= (long)::GetCtlMax(scrollbar);
  338.     
  339.     /*
  340.     // We don't want to scroll to just any pixel, though...
  341.     */
  342.     
  343.     newScroll /= ABUText::TELineHeight(te);
  344.     newScroll *= ABUText::TELineHeight(te);
  345.     
  346.     /*
  347.     // Now find out where we are, and how far we have to move to get to
  348.     // where we want to go.
  349.     */
  350.     
  351.     deltaScroll = ABUText::GetTEVScroll(te) - newScroll;
  352.     ABUText::PinnedTEVScroll(te, scrollbar, deltaScroll);
  353. }
  354.  
  355. /*=============================== ABUText::ScrollTELine ==================================*/
  356. //
  357. // Scroll up or down one line
  358. //
  359. //    is called by:
  360. //        TrackUpDownArrows
  361. //
  362. void ABUText::ScrollTELine(TEHandle te, ControlHandle scrollbar, short dir)
  363. {
  364.     //    metro: ty...addition to trap bad parameters
  365.     if (!(te && scrollbar))
  366.         return;
  367.         
  368.     ABUText::PinnedTEVScroll(te, scrollbar, dir * ABUText::TELineHeight(te));
  369. }
  370.  
  371. /*=============================== ABUText::ScrollTEPage ==================================*/
  372. //
  373. //    Scroll up or down one page
  374. //
  375. //    is called by:
  376. //        TrackUpDownPage
  377. //
  378. void ABUText::ScrollTEPage(TEHandle te, ControlHandle scrollbar, short dir)
  379. {
  380.     short        linePix;
  381.     short        pagePix;
  382.     short        pageLines;
  383.     
  384.     //    metro: ty...addition to trap bad parameters
  385.     if (!(te && scrollbar))
  386.         return;
  387.         
  388.     pagePix = ABUText::ViewHeight(te);
  389.     linePix = ABUText::TELineHeight(te);
  390.     pageLines = pagePix / linePix;
  391.     pagePix = (pageLines - 1) * linePix;
  392.     
  393.     ABUText::PinnedTEVScroll(te, scrollbar, dir * pagePix);
  394. }
  395.  
  396. /*=============================== ABUText::TrackUpDownArrows ==================================*/
  397. //
  398. // This routine is called repeatedly while the scroll bar is tracked.
  399. //
  400. // 1.0a2 SJ added the void return type below:
  401. //
  402. //    is called by:
  403. //        ScrollBarClick
  404. //
  405. pascal void ABUText::TrackUpDownArrows(ControlHandle theControl, short partCode)
  406. {
  407.     TEHandle    te;
  408.     
  409.     //    begin here...
  410.     //    metro: ty...addition to trap bad parameters
  411.     if (!theControl)
  412.         return;
  413.     else
  414.         te = (TEHandle) ::GetCRefCon(theControl);
  415.  
  416.     if((te && theControl))
  417.         switch(partCode)    
  418.         {
  419.             case     inUpButton:
  420.             
  421.                     ABUText::ScrollTELine(te, theControl, kTEUscrollForwardOneUnit);
  422.                     break;
  423.                     
  424.             case inDownButton:
  425.             
  426.                     ABUText::ScrollTELine(te, theControl, kTEUscrollBackwardOneUnit);
  427.                     break;
  428.                     
  429.         }
  430. }
  431.  
  432. /*=============================== ABUText::TrackUpDownPage ==================================*/
  433. //
  434. // This routine is called repeatedly while the scroll bar is tracked.
  435. //
  436. // 1.0a2+ ty created from a copy of TrackUpDownArrows()
  437. //
  438. //    is called by:
  439. //        ScrollBarClick
  440. //
  441. pascal void ABUText::TrackUpDownPage(ControlHandle theControl, short partCode)
  442. {
  443.     TEHandle    te;
  444.     
  445.     //    begin here...
  446.     //    metro: ty...addition to trap bad parameters
  447.     if (!theControl)
  448.         return;
  449.     else
  450.         te = (TEHandle) ::GetCRefCon(theControl);
  451.  
  452.     if((te && theControl))
  453.         switch(partCode) 
  454.         {
  455.             case     inPageUp:
  456.             
  457.                     ABUText::ScrollTEPage(te, theControl, kTEUscrollForwardOneUnit);
  458.                     break;
  459.  
  460.             case     inPageDown:
  461.             
  462.                     ABUText::ScrollTEPage(te, theControl, kTEUscrollBackwardOneUnit);
  463.                     break;
  464.                 
  465.             case     inThumb:
  466.             
  467.                     ABUText::TEVThumbScroll(te, theControl);
  468.                     break;
  469.                 
  470.         } 
  471. }
  472.  
  473. /*=============================== ABUText::ScrollBarClick ==================================*/
  474. //
  475. //    Interact with a VERTICAL scroll bar
  476. //    
  477. //    Pass a handle to the scrollbar's control record and the point (in LOCAL
  478. //    coordinates) where the mouse hit the scrollbar.
  479. //    
  480. //    WARNING:    The refCon of the scrollbar MUST point to the textedit record
  481. //                That it is associated with.
  482. //
  483. //    is called by:
  484. //        external code; this is called by the using-application
  485. //
  486.  
  487. void ABUText::ScrollBarClick(ControlHandle scrollbar, Point where)
  488. {
  489.     TEHandle            te = NULL;
  490.     ControlActionUPP    actionUPP;
  491.  
  492.     /*
  493.     // Ponnuki always sets the refCon of a scrollbar to the
  494.     // TEHandle associated with that scrollbar
  495.     */
  496.     
  497.     if (!scrollbar) 
  498.     {
  499.         return;
  500.     } 
  501.     else if ((*scrollbar)->contrlHilite == 255) 
  502.     {
  503.         //    control is not active, so bail out.
  504.         return;
  505.     }
  506.         
  507.     te = (TEHandle) ::GetCRefCon(scrollbar);
  508.     if (!te)
  509.         return;
  510.         
  511.     /*
  512.     // TrackControl passes different parameters to 'trackAction' based on
  513.     // what type of control it is tracking & what part of the control the
  514.     // mouse is in.  Therefore, we cannot simply use the same trackAction
  515.     // procedure for every 'TrackControl' that might come through this
  516.     // routine.    :<  Fortunately, we only need to call a 'trackAction'
  517.     // proc for the up and down facing buttons.
  518.     */
  519.     
  520.     switch(TestControl(scrollbar,where)) 
  521.     {
  522.         case     inUpButton:
  523.         case     inDownButton:
  524.         
  525.                 //    do the power pc thing!
  526.                 actionUPP = NewControlActionProc(ABUText::TrackUpDownArrows);
  527.                 ::TrackControl(scrollbar,where, actionUPP);
  528.                 DisposeRoutineDescriptor(actionUPP);
  529.                 break;
  530.             
  531.         case     inPageUp:
  532.         case     inPageDown:
  533.         
  534.                 //    do the power pc thing!
  535.                 actionUPP = NewControlActionProc(ABUText::TrackUpDownPage);        //    1.0a4 ty fixed
  536.                 ::TrackControl(scrollbar, where, actionUPP);
  537.                 DisposeRoutineDescriptor(actionUPP);
  538.                 break;
  539.             
  540.         case     inThumb:
  541.         
  542.                 ::TrackControl(scrollbar,where, (ControlActionUPP) 0L);    // 1.0a4 SJ cast
  543.                 ABUText::TEVThumbScroll(te, scrollbar);
  544.                 break;
  545.     }
  546. }
  547.  
  548. /*=============================== ABUText::FixScrollBar ==================================*/
  549. //
  550. //    Fix a VERTICAL scroll bar after a TE field has been scrolled 
  551. //    
  552. //    Pass a handle to the scrollbar's control record
  553. //    
  554. //    WARNING:    The refCon of the scrollbar MUST point to the textedit record
  555. //    That it is associated with.
  556. //
  557. //    is called by:
  558. //        PinnedTEVScroll
  559. //
  560. void ABUText::FixScrollBar(ControlHandle scrollbar)
  561. {
  562.     //TEHandle    te;
  563.  
  564.     /*
  565.     // Ponnuki always sets the refCon of a scrollbar to the
  566.     // TEHandle associated with that scrollbar
  567.     */
  568.     
  569.     if(!scrollbar)
  570.         return;
  571.         
  572.     //te = (TEHandle) GetCRefCon(scrollbar);
  573.     ::SetCtlValue(scrollbar, ABUText::VScrollLocation(scrollbar));
  574.     ::Draw1Control(scrollbar);
  575. }
  576.